package com.kila;

import com.kila.utils.MybatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Test {


    //通过id获取学生对象
     public static void  getStudentById() throws IOException {
         String xml = "config.xml";
         InputStream inputStream;
         inputStream = Resources.getResourceAsStream(xml);
         SqlSessionFactory sqlSessionFactory =
                 new SqlSessionFactoryBuilder().build(inputStream);
         SqlSession session = sqlSessionFactory.openSession();
         Student s = session.selectOne("com.kila.StudentMapper.student1",1);

         System.out.println(s);
         session.close();
     }


     //通过模糊搜索获取学生
    public static void getStudentByName() throws IOException {
         String xml ="config.xml";
         InputStream inputStream = Resources.getResourceAsStream(xml);
         SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
         SqlSession session= sqlSessionFactory.openSession();
       List<Student> list = (List) session.selectList("com.kila.StudentMapper.getStudentsByName","y");
        for (Student student: list
             ) {
            System.out.println(student);
        }

        session.close();



    }


    public static void addStudent(Student student ) throws IOException {
         String xml="config.xml";
         InputStream inputStream =
                 Resources.getResourceAsStream(xml);
         SqlSessionFactory sessionFactory =
                 new SqlSessionFactoryBuilder().build(inputStream);
         SqlSession session = sessionFactory.openSession();


         session.insert("com.kila.",student);

    }


    public static void getStudentByIf(){
       SqlSession session= MybatisUtils.getSession();

       //新建一个Student对象
        Student student = new Student();
//        student.setStuname("zs");
        student.setStuage(12);
        List<Student> students = session.selectList("com.kila.StudentMapper.selStudentByIf",student);
        System.out.println( students.toString());
        session.close();

    }



    public static void getStudentByChoose(){
         SqlSession session  = MybatisUtils.getSession();
         Student student = new Student();
         student.setStuname("s");
          List<Student> students =  session.selectList("com.kila.StudentMapper.selectStudentByChoose",student);
          System.out.println(students);
          session.close();
    }

    public static void getStudentByWhere(){
         SqlSession session = MybatisUtils.getSession();
        Student student = new Student();
//        student.setStuname("zs");
        List<Student> students = session.selectList("com.kila.StudentMapper.selectStudentByWhere",student);
         System.out.println(students);
         session.close();
    }


    public static void getStudentByForeach(){
         SqlSession session = MybatisUtils.getSession();
         List<Integer> list = new ArrayList();
         list.add(1);
         list.add(3);
        list.add(4);
       List<Student> students =  session.selectList("com.kila.StudentMapper.selectStudentByForeach",list);
       System.out.println(students);
       session.close();
    }

    /**
     * 模糊查找 使用bind的  详细请看对应的StudentMapper.xml 内的selectStudentByBind
     */

    public static void getStudentByBind(){
         SqlSession session = MybatisUtils.getSession();
         Student student = new Student();
         student.setStuname("z");
         List<Student> students = session.selectList("com.kila.StudentMapper.selectStudentByBind",student);
         System.out.println(students);
         session.close();
    }


    /**
     * 一对一查询的
     */
    public static void getStudentByOtO(){
        SqlSession session = MybatisUtils.getSession();
        Integer i = 1;
       Student student = session.selectOne("com.kila.StudentMapper.selectStudentByOto",i);
        System.out.println(student);
        session.close();

    }

    public static void getStudentByoTm(){
        SqlSession session = MybatisUtils.getSession();
        List<Student> students =   session.selectList("com.kila.StudentMapper.selectStudentByotm",2);
        for (Student s: students
             ) {
      System.out.println(s.getStuno()+"--"+s.getStuname()+"--"+s.getOrder());

        }
        session.close();
    }



    public static void main(String[] args) throws IOException {
//        getStudentByName();
//        getStudentByIf();
//           getStudentByChoose();
//        getStudentByWhere();
//        getStudentByForeach();
//        getStudentByBind();

//        一对一查询
//           getStudentByOtO();

 //      一对多查询
        getStudentByoTm();
    }
}
